﻿"""Audit logging helper service (Synchronous)."""

from __future__ import annotations

from typing import Any

from sqlalchemy.orm import Session

from ..models.audit_log import AuditLog


class AuditService:
    """Lightweight audit logger writing directly to the database (Synchronous)."""

    def __init__(self, session: Session) -> None:
        self.session = session

    def record(
        self,
        *,
        actor_id: int,
        action: str,
        payload_digest: str | None = None,
        extra_data: dict[str, Any] | None = None,
    ) -> AuditLog:
        log = AuditLog(
            actor_id=actor_id,
            action=action,
            payload_digest=payload_digest,
            extra_data=extra_data,
        )
        self.session.add(log)
        self.session.flush()
        self.session.refresh(log)
        return log
